	<?php
        session_start();
		?>
	<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <link rel="stylesheet" type="text/css" href="style3.css"/>
    </head>
	<?php
        if(isset($_SESSION['lang']))
            require_once "./language_files/signIn_".$_SESSION['lang'].".php";
        else
            require_once "./language_files/signIn_en.php";
      
		include "functions.php";
  
    if(isset($_SESSION['member_id']))
    {
        if(isset($_GET['val']))
            echo"<script type='text/javascript'>top.window.location='main.php?start=1'</script>";
        else
            echo"<script type='text/javascript'>top.window.location='main.php'</script>";
    }
// the if condition check if the user is already logged in through isset
// header is used to transfer the user to his home page if he is already logged in

    if(isset($_GET['dum']))
    {
        echo '<script type="text/javascript">',
        'alert("Your sign up process is almost complete. Please sign in.");',
        '</script>';
    }

    if(isset($_GET['new']))
    {
        $check=mysql_connect("localhost","root","");
        if(!$check)
        {
            die($lang['error_1'] . mysql_error());
        }
        mysql_select_db("mydb",$check);
        $member_id=mysql_real_escape_string($_GET['member_id']);
        $query3=mysql_query("UPDATE `member` SET `activated`=TRUE WHERE m.`member_id`='$member_id'") or die(mysql_error());
    }
    if(isset($_POST['Submit']))
    { //when submit is clicked Signin function will activate
        $check=mysql_connect("localhost","root","");
        if(!$check)
        {
            die($lang['error_1'] . mysql_error());
        }
        mysql_select_db("mydb",$check);
        $email = mysql_real_escape_string($_POST['email']);
        $get_member_id=mysql_query("SELECT m.`member_id` FROM `member`m WHERE m.`email` ='$email'") or die(mysql_error());
        $row2=mysql_fetch_assoc($get_member_id);
        $member_id=$row2['member_id'];

        $getactivated=mysql_query("SELECT activated FROM member WHERE member_id = '$member_id'")
                or die(mysql_error());
        $get_activated=mysql_fetch_assoc($getactivated);
        $activated=$get_activated['activated'];
        if($activated == true)
        {
            Signin();
        }
        else
        {
            echo '<script>',
            'alert("Please check the confirmation link send to your email inorder to Login");',
            '</script>';
        }
    }
// if the user click on submit button he will enter SignIn method
    function Signin()
    {
	global $lang;
// name of SignIn method
        $check=mysql_connect("localhost","root","");
        if(!$check)
        {
            die($lang['error_1'] . mysql_error());
        }
        mysql_select_db("mydb",$check);

// establish database connection
        $email=mysql_real_escape_string($_POST['email']);  //username that will be submit from a form
        $password=mysql_real_escape_string($_POST['password']);  //password that will be submit from a form


        $res=mysql_query("SELECT * FROM member Where email = '$email' AND password = '$password'",$check)  //this is used to retrive the data from our database according to what the user enters
                or die(mysql_error()); //eturns the error description of the last MySQL operation


        if(mysql_num_rows($res) == 1)
        { // if we found the username and password in the database
            $row=mysql_fetch_assoc($res);
            $_SESSION['member_id']=$row['member_id'];
            $_SESSION['email']=mysql_real_escape_string($_POST['email']); // set the session variable to username in order to take it to another page

            if(isset($_GET['val']))
            {

                echo '<script type="text/javascript">',
                "top.window.location='main.php?start=1'",
                '</script>';
            }
            else
            {
                echo '<script type="text/javascript">',
                "top.window.location='main.php'",
                '</script>';
            }
        }
        else
        {
            echo '<script type="text/javascript">',
            'alert("The Combination of username or password are Incorrect");',
            '</script>';
        }
    }
    ?>
    <form id='login' action='' target="_self" method='post' accept-charset='UTF-8' onsubmit="return valid()">
        <fieldset>

            <legend style="font-size: 12pt"><b><?php echo $lang['signIn']; ?></b></legend>
            <table style="border: 0px;">
                <input type='hidden' name='submitted' id='submitted' value='1'/>
                <tr><td style="text-align: center;"><label for='username' ><?php echo $lang['mail']; ?></label></td>
                    <td style="text-align: center;"> <input type='text' name='email' id='username'  maxlength="50" /> </td></tr>
                <tr> <td style="text-align: center;"> <label for='password' ><?php echo $lang['pass']; ?></label></td>
                    <td style="text-align: center; "> <input type='password' name='password' id='password' maxlength="50" /> </td></tr>
            </table>
            <p align="center"><input style="width: 60px;" type='submit' name='Submit' value='<?php echo $lang['login']; ?>' /></p>
            <a href='resetPassword1.php'><?php echo $lang['forgot']; ?></a>

        </fieldset>
    </form>
    <script type='text/javascript'>
        function valid(){
            if(document.getElementById('username').value.toString().length>=1 || document.getElementById('password').value.toString().length>=1){
                return true;
            }
            else{
                alert("The Combination of username and password are Incorrect !");
                return false;
            }
        }

    </script>
</html>